<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    {include file='public/_meta' /}
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="__PUBLIC__static/Layui-KnifeZ/css/layui.css" rel="stylesheet" />
    <script src="__PUBLIC__static/Layui-KnifeZ/layui.js"></script>
    <script src="__PUBLIC__static/ace/ace.js"></script>

</head>
<body>
<div class="layui-fluid">
    <div class="layui-row" style="padding-top: 20px;background-color: white; padding-bottom: 20px; padding-right: 20px;">
        <form class="layui-form" name="submitform" id="submitform" method="post">
            <div class="layui-form-item">
                <label for="classid" class="layui-form-label">
                    <span class="x-red">*</span>所属栏目
                </label>
                <div class="layui-input-inline">
                    <select name="classid" id="classid">
                        {foreach $plist as $k=>$v}
                        <option value="{$v.id}">{$v.title}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red">*</span>文章标题
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title" style="width: 350px" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red"></span>副标题
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="ftitle" name="ftitle" style="width: 350px" lay-verify="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">置顶级别</label>
                    <div class="layui-input-inline">
                        <select name="istop" lay-verify="required">
                            <option value="0">不置顶</option>
                            <option value="1">一级置顶</option>
                            <option value="2">二级置顶</option>
                            <option value="3">三级置顶</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">是否推荐</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="isgood" value="1" title="是">
                        <input type="radio" name="isgood" value="0" title="否" checked>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red"></span>标题图片
                </label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" id="uploadimg">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <input type="hidden" name="titlepic" id="titlepic" required value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red"></span>内容简介
                </label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入简介文字" id="smalltext" name="smalltext" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-inline">
                        <input type="text" id="writer" name="writer" lay-verify="" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">来源</label>
                    <div class="layui-input-inline">
                        <input type="text" id="befrom" name="befrom" lay-verify="" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red"></span>内容
                </label>
                <div class="layui-input-block">
                    <textarea id="newstext"></textarea>
                    <input type="hidden" name="content" id="content">
                </div>
            </div>



            <div class="layui-form-item">
                <div class="layui-input-block">
                <button type="submit" class="layui-btn" id="addBtn" lay-submit="" lay-filter="add">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer','upload','layedit'],
    function() {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        var upload = layui.upload;
        var layedit = layui.layedit;

        layedit.set({
            //暴露layupload参数设置接口 --详细查看layupload参数说明
            uploadImage: {
                url: "{:url('index/index/uploadimg')}",
                accept: '{$uploadset.type}',
                acceptMime: '{$uploadset.type}/!*',
                exts: '{$uploadset.exts}',
                size: '{$uploadset.size}'
            }
            , uploadVideo: {
                url: "{:url('index/index/uploadvideo')}",
                accept: '{$uploadset.vtype}',
                acceptMime: '{$uploadset.vtype}/!*',
                exts: '{$uploadset.vexts}',
                size: '{$uploadset.vsize}'
            }
            ,uploadFiles: {
                url: "{:url('index/index/uploadfile')}",
                accept: '{$uploadset.ftype}',
                acceptMime: '{$uploadset.ftype}/*',
                size: '{$uploadset.fsize}',
                done: function (data) {
                    console.log(data);
                }
            }
            //右键删除图片/视频时的回调参数，post到后台删除服务器文件等操作，
            //传递参数：
            //图片： imgpath --图片路径
            //视频： filepath --视频路径 imgpath --封面路径
            , calldel: {
                url: "{:url('index/index/deletefile')}"
            }
            //新增iframe外置样式和js
            , quote:{
                style: ['__PUBLIC__static/Layui-KnifeZ/css/layui.css','/others'],
                js: ['__PUBLIC__static/Layui-KnifeZ/lay/modules/jquery.js']
            }
            //开发者模式 --默认为false
            , devmode: true
            //插入代码设置
            , codeConfig: {
                hide: true,  //是否显示编码语言选择框
                default: 'javascript' //hide为true时的默认语言格式
            }
            , tool: [
                'html', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'colorpicker', 'face'
                , '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'image_alt', 'video', 'anchors'
                , '|', 'fullScreen'
            ]
            , height: '90%'
            ,onchange:function(content){
                console.log(content);
            }
        });
        var ieditor = layedit.build('newstext');

        //执行实例
        var uploadInst = upload.render({
            elem: '#uploadimg' //绑定元素
            //,name:"img"
            ,accept: '{$uploadset.type}' //只允许上传图片
            ,acceptMime: '{$uploadset.type}/*' //只筛选图片
            ,size: '{$uploadset.size}'//限定大小
            ,url: "{:url('index/index/uploadimg')}" //上传接口
            ,done: function(res, index, upload){
                //上传完毕回调
                //console.log(res);
                document.getElementById('titlepic').value = res.data.src;
                //$('#titlepic').val(res.data.src);
            }
            ,error: function(){
                //请求异常回调
            }
        });

        //console.log();

        //监听提交
        form.on('submit(add)',
            function(data) {
                //console.log(layedit.getContent(ieditor));
                //$('#content').html(layedit.getContent(ieditor));
                //发异步，把数据提交给php
                //console.log(data);
                var formdata = {
                    'classid': data.field.classid,
                    'title' : data.field.title,
                    'ftitle' : data.field.ftitle,
                    'istop' : data.field.istop,
                    'isgood' : data.field.isgood,
                    'titlepic' : data.field.titlepic,
                    'newstext' : layedit.getContent(ieditor),
                    'writer' : data.field.writer,
                    'befrom' : data.field.befrom,
                    'smalltext' : data.field.smalltext,
                };
                console.log(formdata);
                $.ajax({
                    url:"{:url('cms/news/adddo')}",
                    type:"post",
                    dataType:"json",
                    data:formdata,
                    success:function(res){
                        //console.log(res);
                        if(parseInt(res.statusCode)==200){
                            layer.msg(res.message, {
                                    icon: 6
                                },
                                function() {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                }
                            );
                        }else{
                            layer.message(res.message);
                        }
                    }
                })
                /*$.getJSON("{:url('cms/news/adddo')}",formdata,function(res){


                });*/
                return false;
        });





    });

</script>

</body>

</html>
